package com.rahms.server.utils;

import com.rahms.common.constant.AutoFillConstant;
import com.rahms.common.constant.CacheConstant;
import com.rahms.common.constant.JwtClaimsConstant;
import com.rahms.common.constant.MessageConstant;
import org.springframework.stereotype.Component;

/**
 * 常量工具类
 * 提供常量相关的工具方法
 */
@Component
public class ConstantUtil {

    /**
     * 构建用户信息缓存键
     */
    public static String buildUserInfoCacheKey(Long userId) {
        return CacheConstant.USER_INFO_CACHE + userId;
    }

    /**
     * 构建用户信息缓存键（按用户名）
     */
    public static String buildUserInfoCacheKey(String username) {
        return CacheConstant.USER_INFO_CACHE + username;
    }

    /**
     * 构建用户列表缓存键
     */
    public static String buildUserListCacheKey(String role, String keyword) {
        return CacheConstant.USER_LIST_CACHE + role + ":" + keyword;
    }

    /**
     * 构建老人信息缓存键
     */
    public static String buildElderlyInfoCacheKey(Long elderlyId) {
        return CacheConstant.ELDERLY_INFO_CACHE + elderlyId;
    }

    /**
     * 构建老人列表缓存键
     */
    public static String buildElderlyListCacheKey(Long userId) {
        return CacheConstant.ELDERLY_LIST_CACHE + userId;
    }

    /**
     * 构建药品信息缓存键
     */
    public static String buildMedicationInfoCacheKey(Long medicationId) {
        return CacheConstant.MEDICATION_INFO_CACHE + medicationId;
    }

    /**
     * 构建药品列表缓存键
     */
    public static String buildMedicationListCacheKey(Long userId, String keyword) {
        return CacheConstant.MEDICATION_LIST_CACHE + userId + ":" + keyword;
    }

    /**
     * 构建用药计划信息缓存键
     */
    public static String buildDrugPlanInfoCacheKey(Long planId) {
        return CacheConstant.DRUG_PLAN_INFO_CACHE + planId;
    }

    /**
     * 构建用药计划列表缓存键
     */
    public static String buildDrugPlanListCacheKey(Long elderlyId) {
        return CacheConstant.DRUG_PLAN_LIST_CACHE + elderlyId;
    }

    /**
     * 构建活跃用药计划缓存键
     */
    public static String buildActiveDrugPlanCacheKey(Long elderlyId) {
        return CacheConstant.DRUG_PLAN_ACTIVE_CACHE + elderlyId;
    }

    /**
     * 构建健康数据列表缓存键
     */
    public static String buildHealthDataListCacheKey(Long elderlyId, String dataType) {
        return CacheConstant.HEALTH_DATA_LIST_CACHE + elderlyId + ":" + dataType;
    }

    /**
     * 构建健康数据统计缓存键
     */
    public static String buildHealthDataStatisticsCacheKey(Long elderlyId, String dataType) {
        return CacheConstant.HEALTH_DATA_STATISTICS_CACHE + elderlyId + ":" + dataType;
    }

    /**
     * 构建消息列表缓存键
     */
    public static String buildNotificationListCacheKey(Long userId, String type) {
        return CacheConstant.NOTIFICATION_LIST_CACHE + userId + ":" + type;
    }

    /**
     * 构建未读消息数量缓存键
     */
    public static String buildUnreadCountCacheKey(Long userId) {
        return CacheConstant.NOTIFICATION_UNREAD_COUNT_CACHE + userId;
    }

    /**
     * 构建系统消息列表缓存键
     */
    public static String buildSystemMessageListCacheKey(Long userId) {
        return CacheConstant.SYSTEM_MESSAGE_LIST_CACHE + userId;
    }

    /**
     * 构建公告信息缓存键
     */
    public static String buildAnnouncementInfoCacheKey(Long announceId) {
        return CacheConstant.ANNOUNCEMENT_INFO_CACHE + announceId;
    }

    /**
     * 构建公告列表缓存键
     */
    public static String buildAnnouncementListCacheKey(String status) {
        return CacheConstant.ANNOUNCEMENT_LIST_CACHE + status;
    }

    /**
     * 构建最新公告缓存键
     */
    public static String buildLatestAnnouncementCacheKey() {
        return CacheConstant.ANNOUNCEMENT_LATEST_CACHE + "latest";
    }

    /**
     * 构建JWT Token缓存键
     */
    public static String buildJwtTokenCacheKey(String token) {
        return CacheConstant.JWT_TOKEN_CACHE_PREFIX + token;
    }

    /**
     * 构建JWT用户缓存键
     */
    public static String buildJwtUserCacheKey(Long userId) {
        return CacheConstant.JWT_USER_CACHE_PREFIX + userId;
    }

    /**
     * 获取缓存过期时间
     */
    public static long getCacheExpireTime(String type) {
        switch (type.toLowerCase()) {
            case "short":
                return CacheConstant.CACHE_EXPIRE_SHORT;
            case "medium":
                return CacheConstant.CACHE_EXPIRE_MEDIUM;
            case "long":
                return CacheConstant.CACHE_EXPIRE_LONG;
            case "very_long":
                return CacheConstant.CACHE_EXPIRE_VERY_LONG;
            case "jwt_token":
                return CacheConstant.JWT_TOKEN_EXPIRE;
            case "user_session":
                return CacheConstant.USER_SESSION_EXPIRE;
            case "statistics":
                return CacheConstant.STATISTICS_CACHE_EXPIRE;
            default:
                return CacheConstant.CACHE_EXPIRE_MEDIUM;
        }
    }

    /**
     * 获取JWT声明字段名
     */
    public static String getJwtClaimField(String field) {
        switch (field.toLowerCase()) {
            case "emp_id":
                return JwtClaimsConstant.EMP_ID;
            case "user_id":
                return JwtClaimsConstant.USER_ID;
            case "phone":
                return JwtClaimsConstant.PHONE;
            case "username":
                return JwtClaimsConstant.USERNAME;
            case "name":
                return JwtClaimsConstant.NAME;
            default:
                return field;
        }
    }

    /**
     * 获取自动填充方法名
     */
    public static String getAutoFillMethod(String method) {
        switch (method.toLowerCase()) {
            case "create_time":
                return AutoFillConstant.SET_CREATE_TIME;
            case "update_time":
                return AutoFillConstant.SET_UPDATE_TIME;
            case "create_user":
                return AutoFillConstant.SET_CREATE_USER;
            case "update_user":
                return AutoFillConstant.SET_UPDATE_USER;
            default:
                return method;
        }
    }

    /**
     * 获取消息常量
     */
    public static String getMessage(String key) {
        switch (key.toLowerCase()) {
            case "password_error":
                return MessageConstant.PASSWORD_ERROR;
            case "account_not_found":
                return MessageConstant.ACCOUNT_NOT_FOUND;
            case "login_success":
                return MessageConstant.LOGIN_SUCCESS;
            case "register_success":
                return MessageConstant.REGISTER_SUCCESS;
            case "user_not_exist":
                return MessageConstant.USER_NOT_EXIST;
            case "username_exists":
                return MessageConstant.USERNAME_EXISTS;
            case "operation_success":
                return MessageConstant.OPERATION_SUCCESS;
            case "operation_failed":
                return MessageConstant.OPERATION_FAILED;
            case "save_success":
                return MessageConstant.SAVE_SUCCESS;
            case "save_failed":
                return MessageConstant.SAVE_FAILED;
            case "update_success":
                return MessageConstant.UPDATE_SUCCESS;
            case "update_failed":
                return MessageConstant.UPDATE_FAILED;
            case "delete_success":
                return MessageConstant.DELETE_SUCCESS;
            case "delete_failed":
                return MessageConstant.DELETE_FAILED;
            case "query_success":
                return MessageConstant.QUERY_SUCCESS;
            case "query_failed":
                return MessageConstant.QUERY_FAILED;
            case "system_error":
                return MessageConstant.SYSTEM_ERROR;
            case "permission_denied":
                return MessageConstant.PERMISSION_DENIED;
            default:
                return key;
        }
    }
}
